﻿using SRB.Tg.DrawingStandard;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Model
{
    public class DrawQuantityTable
    {
        static string _dwgPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Template\\RebarGrade.dwg";
        static double rowH = 6;

        /// <summary>
        /// 单个构造材料数量明细表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="startPoint"></param>
        /// <param name="dt"></param>
        /// <param name="headName"></param>
        /// <param name="rowNum"></param>
        /// <param name="colNum"></param>
        /// <param name="tbHeight"></param>
        /// <param name="tbWidth"></param>
        /// <param name="txtStyle"></param>
        /// <param name="scale"></param>
        public static void DrawSpanMaterialsTable(Database db, ref List<Extents2d> extLst, Point3d startPoint, System.Data.DataTable dtQu, List<string> headName,
         string txtStyle, double scale, string title)
        {
            double width = 0;

            List<Tuple<System.Data.DataTable, int, int>> tuLst = new List<Tuple<System.Data.DataTable, int, int>>();
            int tbRow = 38;
            DivTable(dtQu, tbRow, out tuLst);
            int pageTbNum = 2;
            int page = tuLst.Count / pageTbNum;
            int pageNum = page * pageTbNum >= tuLst.Count ? page : page + 1;
            int colNum = headName.Count;
            if (tuLst.Count > 0)
            {
                Extents2d ext = new Extents2d(startPoint.C2D(), startPoint.C2D());
                for (int j = 0; j < pageNum; j++)
                {
                    startPoint = startPoint.C3D(420 * scale * j);
                    ext = new Extents2d(startPoint.C2D(), startPoint.C2D());
                    for (int n = pageTbNum * j; n < tuLst.Count; n++)
                    {
                        System.Data.DataTable dt = tuLst[n].Item1;
                        int rowNum = tuLst[n].Item2 + 1;
                        if (n < (j + 1) * pageTbNum)
                        {
                            if (n == (j + 1) * pageTbNum - 1)
                            {
                                #region 单页最后一张表
                                using (Transaction tr = db.TransactionManager.StartTransaction())
                                {
                                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                                        OpenMode.ForWrite) as BlockTableRecord;
                                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                                    ObjectId id;
                                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                                    id = st[txtStyle];
                                    // 创建table 
                                    Table tb = new Table();
                                    tb.SetSize(rowNum, colNum);       // 设置几行几列
                                    tb.Position = startPoint.C3D(ext.W() + 10 * scale);
                                    tb.GenerateLayout();

                                    // 设置表头
                                    for (int i = 0; i < colNum; ++i)
                                    {
                                        tb.Cells[0, i].TextString = headName[i]; //获取i行j列数据
                                        tb.Cells[0, i].Contents[0].TextHeight = 3 * scale;
                                        tb.Cells[0, i].Alignment = CellAlignment.MiddleCenter;
                                        tb.Cells[0, i].Contents[0].Rotation = 0;
                                        tb.Cells[0, i].Borders.Horizontal.Margin = 2 * scale;
                                        tb.Cells[0, i].Contents[0].TextStyleId = id;
                                        if (i == 0)
                                            tb.MergeCells(CellRange.Create(tb, 0, 0, 0, 1));
                                    }
                                    tb.Rows[0].Height = 8 * scale;
                                    string value = " ";

                                    if (dt.Rows.Count > 0)
                                    {
                                        for (int m = 1; m < rowNum; m++)
                                        {
                                            System.Data.DataRow dr = dt.Rows[m - 1];
                                            for (int i = 0; i < colNum; ++i)
                                            {
                                                double weight = 0;
                                                switch (i)
                                                {
                                                    case 0:
                                                        value = dr["materiel"].ToString();
                                                        break;
                                                    case 1:
                                                        value = dr["materiel_code"].ToString();
                                                        break;
                                                    case 2:
                                                        value = dr["materiel_specs"].ToString().Replace("φ", "Φ").Replace("X", "×") + "×" + (double.Parse(dr["length"].ToString()) * 1000).ToString("0.0");
                                                        break;
                                                    case 3:
                                                        value = double.Parse(dr["unit_structural_weight"].ToString()).ToString("0.000");
                                                        break;
                                                    case 4:
                                                        value = dr["number"].ToString();
                                                        break;
                                                    case 5:
                                                        value = double.Parse(dr["total_weight"].ToString()).ToString("0.000");
                                                        break;
                                                    case 6:
                                                        value = dr["remark"].ToString();
                                                        break;
                                                }

                                                tb.Cells[m, i].TextString = value; //获取i行j列数据
                                                tb.Cells[m, i].Contents[0].TextHeight = 2.5 * scale;
                                                tb.Cells[m, i].Alignment = CellAlignment.MiddleCenter;
                                                tb.Cells[m, i].Contents[0].Rotation = 0;
                                                tb.Cells[m, i].Borders.Horizontal.Margin = 1 * scale;
                                                tb.Cells[m, i].Contents[0].TextStyleId = id;
                                            }
                                        }
                                    }

                                    // 设置列宽,行高
                                    for (int i = 0; i < colNum; ++i)
                                    {
                                        if (i == 0 || i == 1)
                                        {
                                            tb.Columns[i].Width = 15 * scale;
                                        }
                                        else if (i == 2)
                                        {
                                            tb.Columns[i].Width = 30 * scale;
                                        }
                                        else
                                        {
                                            tb.Columns[i].Width = 25 * scale;
                                        }
                                    }

                                    for (int i = 1; i < rowNum; ++i)
                                    {
                                        tb.Rows[i].Height = 6 * scale;
                                    }
                                    List<Tuple<int, int>> tuMergeLst;
                                    int index = 0;
                                    CalMergeInfo(dt, "materiel", out tuMergeLst);
                                    List<CellRange> cellList = new List<CellRange>();
                                    for (int i = 0; i < tuMergeLst.Count; i++)
                                    {
                                        CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                                        cellList.Add(range);
                                    }
                                    foreach (CellRange range in cellList)
                                    {
                                        tb.MergeCells(range);
                                    }
                                    index = 1;
                                    CalMergeInfo(dt, "materiel_code", out tuMergeLst);
                                    cellList = new List<CellRange>();
                                    for (int i = 0; i < tuMergeLst.Count; i++)
                                    {
                                        CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                                        cellList.Add(range);
                                    }
                                    foreach (CellRange range in cellList)
                                    {
                                        tb.MergeCells(range);
                                    }

                                    tb.LayerId = lt["H细线"];
                                    modelSpace.AppendEntity(tb);
                                    tr.AddNewlyCreatedDBObject(tb, true);
                                    ext = ext.Add(new Extents2d(tb.Bounds.Value.MinPoint.C2D(), tb.Bounds.Value.MaxPoint.C2D()));

                                    List<Point2d> pList = new List<Point2d>();
                                    Point2d pt1 = tb.Position.C2D();
                                    pList.Add(pt1);
                                    Point2d pt2 = pt1.C2D(0, -tb.Height);
                                    pList.Add(pt2);
                                    Point2d pt3 = pt1.C2D(tb.Width, -tb.Height);
                                    pList.Add(pt3);
                                    Point2d pt4 = pt1.C2D(tb.Width, 0);
                                    pList.Add(pt4);
                                    PLPloter.AddPolylineByPointList(db, pList, ref ext, "H粗线", true);

                                    if (n == 0)
                                        TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title, scale);
                                    else
                                        TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title + "(续" + n + ")", scale);
                                    tr.Commit();
                                }
                                #endregion
                            }
                            else
                            {
                                #region 表
                                using (Transaction tr = db.TransactionManager.StartTransaction())
                                {
                                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                                        OpenMode.ForWrite) as BlockTableRecord;
                                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                                    ObjectId id;
                                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                                    id = st[txtStyle];
                                    // 创建table 
                                    Table tb = new Table();
                                    tb.SetSize(rowNum, colNum);       // 设置几行几列
                                    tb.Position = startPoint;
                                    tb.GenerateLayout();

                                    // 设置表头
                                    for (int i = 0; i < colNum; ++i)
                                    {
                                        tb.Cells[0, i].TextString = headName[i]; //获取i行j列数据
                                        tb.Cells[0, i].Contents[0].TextHeight = 3 * scale;
                                        tb.Cells[0, i].Alignment = CellAlignment.MiddleCenter;
                                        tb.Cells[0, i].Contents[0].Rotation = 0;
                                        tb.Cells[0, i].Borders.Horizontal.Margin = 2 * scale;
                                        tb.Cells[0, i].Contents[0].TextStyleId = id;
                                        if (i == 0)
                                            tb.MergeCells(CellRange.Create(tb, 0, 0, 0, 1));
                                    }
                                    tb.Rows[0].Height = 8 * scale;
                                    string value = " ";

                                    if (dt.Rows.Count > 0)
                                    {
                                        for (int m = 1; m < rowNum; m++)
                                        {
                                            System.Data.DataRow dr = dt.Rows[m - 1];
                                            for (int i = 0; i < colNum; ++i)
                                            {
                                                double weight = 0;
                                                switch (i)
                                                {
                                                    case 0:
                                                        value = dr["materiel"].ToString();
                                                        break;
                                                    case 1:
                                                        value = dr["materiel_code"].ToString();
                                                        break;
                                                    case 2:
                                                        value = dr["materiel_specs"].ToString().Replace("φ", "Φ").Replace("X", "×") + "×" + (double.Parse(dr["length"].ToString()) * 1000).ToString("0.0");
                                                        break;
                                                    case 3:
                                                        value = double.Parse(dr["unit_structural_weight"].ToString()).ToString("0.000");
                                                        break;
                                                    case 4:
                                                        value = dr["number"].ToString();
                                                        break;
                                                    case 5:
                                                        value = double.Parse(dr["total_weight"].ToString()).ToString("0.000");
                                                        break;
                                                    case 6:
                                                        value = dr["remark"].ToString();
                                                        break;
                                                }

                                                tb.Cells[m, i].TextString = value; //获取i行j列数据
                                                tb.Cells[m, i].Contents[0].TextHeight = 2.5 * scale;
                                                tb.Cells[m, i].Alignment = CellAlignment.MiddleCenter;
                                                tb.Cells[m, i].Contents[0].Rotation = 0;
                                                tb.Cells[m, i].Borders.Horizontal.Margin = 1 * scale;
                                                tb.Cells[m, i].Contents[0].TextStyleId = id;
                                            }
                                        }
                                    }

                                    // 设置列宽,行高
                                    for (int i = 0; i < colNum; ++i)
                                    {
                                        if (i == 0 || i == 1)
                                        {
                                            tb.Columns[i].Width = 15 * scale;
                                        }
                                        else if (i == 2)
                                        {
                                            tb.Columns[i].Width = 30 * scale;
                                        }
                                        else
                                        {
                                            tb.Columns[i].Width = 25 * scale;
                                        }
                                    }

                                    for (int i = 1; i < rowNum; ++i)
                                    {
                                        tb.Rows[i].Height = 6 * scale;
                                    }
                                    #region  合并单元格
                                    List<Tuple<int, int>> tuMergeLst;
                                    int index = 0;
                                    CalMergeInfo(dt, "materiel", out tuMergeLst);
                                    List<CellRange> cellList = new List<CellRange>();
                                    for (int i = 0; i < tuMergeLst.Count; i++)
                                    {
                                        CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                                        cellList.Add(range);
                                    }
                                    foreach (CellRange range in cellList)
                                    {
                                        tb.MergeCells(range);
                                    }
                                    index = 1;
                                    CalMergeInfo(dt, "materiel_code", out tuMergeLst);
                                    cellList = new List<CellRange>();
                                    for (int i = 0; i < tuMergeLst.Count; i++)
                                    {
                                        CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                                        cellList.Add(range);
                                    }
                                    foreach (CellRange range in cellList)
                                    {
                                        tb.MergeCells(range);
                                    }
                                    #endregion

                                    tb.LayerId = lt["H细线"];
                                    modelSpace.AppendEntity(tb);
                                    tr.AddNewlyCreatedDBObject(tb, true);
                                    ext = ext.Add(new Extents2d(tb.Bounds.Value.MinPoint.C2D(), tb.Bounds.Value.MaxPoint.C2D()));

                                    List<Point2d> pList = new List<Point2d>();
                                    Point2d pt1 = tb.Position.C2D();
                                    pList.Add(pt1);
                                    Point2d pt2 = pt1.C2D(0, -tb.Height);
                                    pList.Add(pt2);
                                    Point2d pt3 = pt1.C2D(tb.Width, -tb.Height);
                                    pList.Add(pt3);
                                    Point2d pt4 = pt1.C2D(tb.Width, 0);
                                    pList.Add(pt4);
                                    PLPloter.AddPolylineByPointList(db, pList, ref ext, "H粗线", true);
                                    if (n == 0)
                                        TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title, scale);
                                    else
                                        TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title + "(续" + n + ")", scale);
                                    tr.Commit();

                                }
                                #endregion
                            }
                        }

                    }
                    extLst.Add(ext);
                }
            }
        }

        public static void DrawSpanMaterialsTableNew(Database db, ref List<Extents2d> extLst, Point3d startPoint, System.Data.DataTable dtQu, System.Data.DataTable dtM, List<string> headName, List<string> headNameM,
 string txtStyle, double scale, string title, string titleM)
        {
            double width = 0;

            List<Tuple<System.Data.DataTable, int, int>> tuLst = new List<Tuple<System.Data.DataTable, int, int>>();
            int tbRow = 38;
            DivTable(dtQu, tbRow, out tuLst);
            int pageTbNum = 2;
            int page = (tuLst.Count+1) / pageTbNum;
            int pageNum = page * pageTbNum >= tuLst.Count+1 ? page : page + 1;
            int colNum = headName.Count;
            bool isDrawingMerge = false;
            if (tuLst.Count > 0)
            {
                Extents2d ext = new Extents2d(startPoint.C2D(), startPoint.C2D());
                for (int j = 0; j < pageNum; j++)
                {
                    if (!isDrawingMerge)
                    {
                        startPoint = startPoint.C3D(420 * scale * j);
                        ext = new Extents2d(startPoint.C2D(), startPoint.C2D());
                        for (int n = pageTbNum * j; n < tuLst.Count + 1; n++)
                        {

                            if (n < (j + 1) * pageTbNum)
                            {
                                if (n == (j + 1) * pageTbNum - 1)
                                {
                                    Extents2d extLast = new Extents2d(startPoint.C2D(ext.W() + 10 * scale), startPoint.C2D(ext.W() + 10 * scale));
                                    if (n < tuLst.Count)
                                    {
                                        Point3d ptMerge = startPoint.C3D(ext.W() + 10 * scale);
                                       System.Data.DataTable dt = tuLst[n].Item1;
                                        int rowNum = tuLst[n].Item2 + 1;
                                        #region 单页最后一张表
                                        using (Transaction tr = db.TransactionManager.StartTransaction())
                                        {
                                            BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                                            BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;
                                            LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                                            ObjectId id;
                                            TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                                            id = st[txtStyle];
                                            // 创建table 
                                            Table tb = new Table();
                                            tb.SetSize(rowNum, colNum);       // 设置几行几列
                                            tb.Position = startPoint.C3D(ext.W() + 10 * scale);
                                            tb.GenerateLayout();

                                            // 设置表头
                                            for (int i = 0; i < colNum; ++i)
                                            {
                                                tb.Cells[0, i].TextString = headName[i]; //获取i行j列数据
                                                tb.Cells[0, i].Contents[0].TextHeight = 3 * scale;
                                                tb.Cells[0, i].Alignment = CellAlignment.MiddleCenter;
                                                tb.Cells[0, i].Contents[0].Rotation = 0;
                                                tb.Cells[0, i].Borders.Horizontal.Margin = 2 * scale;
                                                tb.Cells[0, i].Contents[0].TextStyleId = id;
                                                if (i == 0)
                                                    tb.MergeCells(CellRange.Create(tb, 0, 0, 0, 1));
                                            }
                                            tb.Rows[0].Height = 8 * scale;
                                            string value = " ";

                                            if (dt.Rows.Count > 0)
                                            {
                                                for (int m = 1; m < rowNum; m++)
                                                {
                                                    System.Data.DataRow dr = dt.Rows[m - 1];
                                                    for (int i = 0; i < colNum; ++i)
                                                    {
                                                        double weight = 0;
                                                        switch (i)
                                                        {
                                                            case 0:
                                                                value = dr["materiel"].ToString();
                                                                break;
                                                            case 1:
                                                                value = dr["materiel_code"].ToString();
                                                                break;
                                                            case 2:
                                                                value = dr["materiel_specs"].ToString().Replace("φ", "Φ").Replace("X", "×") + "×" + (double.Parse(dr["length"].ToString()) * 1000).ToString("0.0");
                                                                break;
                                                            case 3:
                                                                value = double.Parse(dr["unit_structural_weight"].ToString()).ToString("0.000");
                                                                break;
                                                            case 4:
                                                                value = dr["number"].ToString();
                                                                break;
                                                            case 5:
                                                                value = double.Parse(dr["total_weight"].ToString()).ToString("0.000");
                                                                break;
                                                            case 6:
                                                                value = dr["remark"].ToString();
                                                                break;
                                                        }

                                                        tb.Cells[m, i].TextString = value; //获取i行j列数据
                                                        tb.Cells[m, i].Contents[0].TextHeight = 2.5 * scale;
                                                        tb.Cells[m, i].Alignment = CellAlignment.MiddleCenter;
                                                        tb.Cells[m, i].Contents[0].Rotation = 0;
                                                        tb.Cells[m, i].Borders.Horizontal.Margin = 1 * scale;
                                                        tb.Cells[m, i].Contents[0].TextStyleId = id;
                                                    }
                                                }
                                            }

                                            // 设置列宽,行高
                                            for (int i = 0; i < colNum; ++i)
                                            {
                                                if (i == 0 || i == 1)
                                                {
                                                    tb.Columns[i].Width = 15 * scale;
                                                }
                                                else if (i == 2)
                                                {
                                                    tb.Columns[i].Width = 30 * scale;
                                                }
                                                else
                                                {
                                                    tb.Columns[i].Width = 25 * scale;
                                                }
                                            }

                                            for (int i = 1; i < rowNum; ++i)
                                            {
                                                tb.Rows[i].Height = 6 * scale;
                                            }
                                            List<Tuple<int, int>> tuMergeLst;
                                            int index = 0;
                                            CalMergeInfo(dt, "materiel", out tuMergeLst);
                                            List<CellRange> cellList = new List<CellRange>();
                                            for (int i = 0; i < tuMergeLst.Count; i++)
                                            {
                                                CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                                                cellList.Add(range);
                                            }
                                            foreach (CellRange range in cellList)
                                            {
                                                tb.MergeCells(range);
                                            }
                                            index = 1;
                                            CalMergeInfo(dt, "materiel_code", out tuMergeLst);
                                            cellList = new List<CellRange>();
                                            for (int i = 0; i < tuMergeLst.Count; i++)
                                            {
                                                CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                                                cellList.Add(range);
                                            }
                                            foreach (CellRange range in cellList)
                                            {
                                                tb.MergeCells(range);
                                            }

                                            tb.LayerId = lt["H细线"];
                                            modelSpace.AppendEntity(tb);
                                            tr.AddNewlyCreatedDBObject(tb, true);
                                            ext = ext.Add(new Extents2d(tb.Bounds.Value.MinPoint.C2D(), tb.Bounds.Value.MaxPoint.C2D()));
                                            extLast = extLast.Add(new Extents2d(tb.Bounds.Value.MinPoint.C2D(), tb.Bounds.Value.MaxPoint.C2D()));
                                            List<Point2d> pList = new List<Point2d>();
                                            Point2d pt1 = tb.Position.C2D();
                                            pList.Add(pt1);
                                            Point2d pt2 = pt1.C2D(0, -tb.Height);
                                            pList.Add(pt2);
                                            Point2d pt3 = pt1.C2D(tb.Width, -tb.Height);
                                            pList.Add(pt3);
                                            Point2d pt4 = pt1.C2D(tb.Width, 0);
                                            pList.Add(pt4);
                                            PLPloter.AddPolylineByPointList(db, pList, ref ext, "H粗线", true);

                                            if (n == 0)
                                                TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title, scale);
                                            else
                                                TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title + "(续" + n + ")", scale);
                                            tr.Commit();
                                        }
                                        #endregion

                                        if (n == tuLst.Count-1&&297 - extLast.L() - Extensions.bottomHeight - Extensions.topHeight > 50)
                                        {
                                            ptMerge = ptMerge.C3D(0, -extLast.L() -9 * scale);
                                            DrawSpanMaterialsTotalTable(db, ref ext, ptMerge, dtM, headNameM, txtStyle, scale, titleM);
                                            isDrawingMerge = true;
                                        }
                                    }
                                    else
                                    {
                                        Point3d ptMerge = startPoint.C3D(ext.W() + 10 * scale);
                                        DrawSpanMaterialsTotalTable(db, ref ext, ptMerge, dtM, headNameM, txtStyle, scale, titleM);
                                    }
                                }
                                else
                                {
                                    if (n < tuLst.Count)
                                    {
                                        System.Data.DataTable dt = tuLst[n].Item1;
                                        int rowNum = tuLst[n].Item2 + 1;
                                        #region 表
                                        using (Transaction tr = db.TransactionManager.StartTransaction())
                                        {
                                            BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                                            BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;
                                            LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                                            ObjectId id;
                                            TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                                            id = st[txtStyle];
                                            // 创建table 
                                            Table tb = new Table();
                                            tb.SetSize(rowNum, colNum);       // 设置几行几列
                                            tb.Position = startPoint;
                                            tb.GenerateLayout();

                                            // 设置表头
                                            for (int i = 0; i < colNum; ++i)
                                            {
                                                tb.Cells[0, i].TextString = headName[i]; //获取i行j列数据
                                                tb.Cells[0, i].Contents[0].TextHeight = 3 * scale;
                                                tb.Cells[0, i].Alignment = CellAlignment.MiddleCenter;
                                                tb.Cells[0, i].Contents[0].Rotation = 0;
                                                tb.Cells[0, i].Borders.Horizontal.Margin = 2 * scale;
                                                tb.Cells[0, i].Contents[0].TextStyleId = id;
                                                if (i == 0)
                                                    tb.MergeCells(CellRange.Create(tb, 0, 0, 0, 1));
                                            }
                                            tb.Rows[0].Height = 8 * scale;
                                            string value = " ";

                                            if (dt.Rows.Count > 0)
                                            {
                                                for (int m = 1; m < rowNum; m++)
                                                {
                                                    System.Data.DataRow dr = dt.Rows[m - 1];
                                                    for (int i = 0; i < colNum; ++i)
                                                    {
                                                        double weight = 0;
                                                        switch (i)
                                                        {
                                                            case 0:
                                                                value = dr["materiel"].ToString();
                                                                break;
                                                            case 1:
                                                                value = dr["materiel_code"].ToString();
                                                                break;
                                                            case 2:
                                                                value = dr["materiel_specs"].ToString().Replace("φ", "Φ").Replace("X", "×") + "×" + (double.Parse(dr["length"].ToString()) * 1000).ToString("0.0");
                                                                break;
                                                            case 3:
                                                                value = double.Parse(dr["unit_structural_weight"].ToString()).ToString("0.000");
                                                                break;
                                                            case 4:
                                                                value = dr["number"].ToString();
                                                                break;
                                                            case 5:
                                                                value = double.Parse(dr["total_weight"].ToString()).ToString("0.000");
                                                                break;
                                                            case 6:
                                                                value = dr["remark"].ToString();
                                                                break;
                                                        }

                                                        tb.Cells[m, i].TextString = value; //获取i行j列数据
                                                        tb.Cells[m, i].Contents[0].TextHeight = 2.5 * scale;
                                                        tb.Cells[m, i].Alignment = CellAlignment.MiddleCenter;
                                                        tb.Cells[m, i].Contents[0].Rotation = 0;
                                                        tb.Cells[m, i].Borders.Horizontal.Margin = 1 * scale;
                                                        tb.Cells[m, i].Contents[0].TextStyleId = id;
                                                    }
                                                }
                                            }

                                            // 设置列宽,行高
                                            for (int i = 0; i < colNum; ++i)
                                            {
                                                if (i == 0 || i == 1)
                                                {
                                                    tb.Columns[i].Width = 15 * scale;
                                                }
                                                else if (i == 2)
                                                {
                                                    tb.Columns[i].Width = 30 * scale;
                                                }
                                                else
                                                {
                                                    tb.Columns[i].Width = 25 * scale;
                                                }
                                            }

                                            for (int i = 1; i < rowNum; ++i)
                                            {
                                                tb.Rows[i].Height = 6 * scale;
                                            }
                                            #region  合并单元格
                                            List<Tuple<int, int>> tuMergeLst;
                                            int index = 0;
                                            CalMergeInfo(dt, "materiel", out tuMergeLst);
                                            List<CellRange> cellList = new List<CellRange>();
                                            for (int i = 0; i < tuMergeLst.Count; i++)
                                            {
                                                CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                                                cellList.Add(range);
                                            }
                                            foreach (CellRange range in cellList)
                                            {
                                                tb.MergeCells(range);
                                            }
                                            index = 1;
                                            CalMergeInfo(dt, "materiel_code", out tuMergeLst);
                                            cellList = new List<CellRange>();
                                            for (int i = 0; i < tuMergeLst.Count; i++)
                                            {
                                                CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                                                cellList.Add(range);
                                            }
                                            foreach (CellRange range in cellList)
                                            {
                                                tb.MergeCells(range);
                                            }
                                            #endregion

                                            tb.LayerId = lt["H细线"];
                                            modelSpace.AppendEntity(tb);
                                            tr.AddNewlyCreatedDBObject(tb, true);
                                            ext = ext.Add(new Extents2d(tb.Bounds.Value.MinPoint.C2D(), tb.Bounds.Value.MaxPoint.C2D()));

                                            List<Point2d> pList = new List<Point2d>();
                                            Point2d pt1 = tb.Position.C2D();
                                            pList.Add(pt1);
                                            Point2d pt2 = pt1.C2D(0, -tb.Height);
                                            pList.Add(pt2);
                                            Point2d pt3 = pt1.C2D(tb.Width, -tb.Height);
                                            pList.Add(pt3);
                                            Point2d pt4 = pt1.C2D(tb.Width, 0);
                                            pList.Add(pt4);
                                            PLPloter.AddPolylineByPointList(db, pList, ref ext, "H粗线", true);
                                            if (n == 0)
                                                TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title, scale);
                                            else
                                                TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title + "(续" + n + ")", scale);
                                            tr.Commit();

                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        Point3d ptMerge = startPoint.C3D();
                                        DrawSpanMaterialsTotalTable(db, ref ext, ptMerge, dtM, headNameM, txtStyle, scale, titleM);
                                    }
                                }
                            }

                        }
                        extLst.Add(ext);
                    }
                }
            }
        }

        public static void DrawSpanMaterialsTotalTable(Database db, ref Extents2d ext, Point3d startPoint, System.Data.DataTable dt, List<string> headName,
    string txtStyle, double scale, string title)
        {
            double width = 0;
            int colNum = headName.Count;
            //ext = new Extents2d(startPoint.C2D(), startPoint.C2D());
            int rowNum = dt.Rows.Count + 1;

            #region 单页最后一张表
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                ObjectId id;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                id = st[txtStyle];
                // 创建table 
                Table tb = new Table();
                tb.SetSize(rowNum, colNum);       // 设置几行几列
                tb.Position = startPoint.C3D();
                tb.GenerateLayout();

                // 设置表头
                for (int i = 0; i < colNum; ++i)
                {
                    tb.Cells[0, i].TextString = headName[i]; //获取i行j列数据
                    tb.Cells[0, i].Contents[0].TextHeight = 3 * scale;
                    tb.Cells[0, i].Alignment = CellAlignment.MiddleCenter;
                    tb.Cells[0, i].Contents[0].Rotation = 0;
                    tb.Cells[0, i].Borders.Horizontal.Margin = 2 * scale;
                    tb.Cells[0, i].Contents[0].TextStyleId = id;
                    if (i == 0)
                        tb.MergeCells(CellRange.Create(tb, 0, 0, 0, 1));
                }
                tb.Rows[0].Height = 8 * scale;
                string value = " ";
                double weight = 0;
                if (dt.Rows.Count > 0)
                {
                    for (int m = 1; m < rowNum; m++)
                    {
                        System.Data.DataRow dr = dt.Rows[m - 1];
                        weight += double.Parse(dr["total_weight"].ToString()) * 0.001;
                        for (int i = 0; i < colNum; ++i)
                        {
                           
                            switch (i)
                            {
                                case 0:
                                    value = dr["materiel"].ToString();
                                    break;
                                case 1:
                                    value = dr["materiel_code"].ToString();
                                    break;
                                case 2:
                                    value = dr["materiel_specs"].ToString().Replace("φ", "Φ").Replace("X", "×") ;
                                    break;                              
                                case 3:
                                    value = double.Parse(dr["total_weight"].ToString()).ToString("0.000");
                                    break;                               
                            }

                            tb.Cells[m, i].TextString = value; //获取i行j列数据
                            tb.Cells[m, i].Contents[0].TextHeight = 2.5 * scale;
                            tb.Cells[m, i].Alignment = CellAlignment.MiddleCenter;
                            tb.Cells[m, i].Contents[0].Rotation = 0;
                            tb.Cells[m, i].Borders.Horizontal.Margin = 1 * scale;
                            tb.Cells[m, i].Contents[0].TextStyleId = id;
                        }
                    }
                }

                tb.Cells[1,5].TextString = weight.ToString("0.000");  //获取i行j列数据
                tb.Cells[1,5].Contents[0].TextHeight = 2.5 * scale;
                tb.Cells[1,5].Alignment = CellAlignment.MiddleCenter;
                tb.Cells[1,5].Contents[0].Rotation = 0;
                tb.Cells[1,5].Borders.Horizontal.Margin = 1 * scale;
                tb.Cells[1,5].Contents[0].TextStyleId = id;
                List<Tuple<int, int>> tuMergeLst;
                int index = 0;
                CalMergeInfo(dt, "materiel", "materiel_code", out tuMergeLst);
                List<CellRange> cellList = new List<CellRange>();
                for (int i = 0; i < tuMergeLst.Count; i++)
                {
                    double weightM = 0;
                    if (tuMergeLst[i].Item1 == tuMergeLst[i].Item2)
                    {
                        weightM += double.Parse(dt.Rows[tuMergeLst[i].Item1]["total_weight"].ToString()) * 0.001;
                    }
                    else
                    {
                        for (int m = tuMergeLst[i].Item1; m < tuMergeLst[i].Item2+1; m++)
                        {
                            weightM += double.Parse(dt.Rows[m]["total_weight"].ToString()) * 0.001;
                        }
                    }
                    tb.Cells[tuMergeLst[i].Item1 + 1,4].TextString = weightM.ToString("0.000");  //获取i行j列数据
                    tb.Cells[tuMergeLst[i].Item1 + 1,4].Contents[0].TextHeight = 2.5 * scale;
                    tb.Cells[tuMergeLst[i].Item1 + 1,4].Alignment = CellAlignment.MiddleCenter;
                    tb.Cells[tuMergeLst[i].Item1 + 1,4].Contents[0].Rotation = 0;
                    tb.Cells[tuMergeLst[i].Item1 + 1,4].Borders.Horizontal.Margin = 1 * scale;
                    tb.Cells[tuMergeLst[i].Item1 + 1,4].Contents[0].TextStyleId = id;
                    if (tuMergeLst[i].Item1 != tuMergeLst[i].Item2)
                    {
                        CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2+1, index);
                        cellList.Add(range);
                        range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index+1, tuMergeLst[i].Item2 + 1, index+1);
                        cellList.Add(range);
                        CellRange range1 = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, 4, tuMergeLst[i].Item2+1, 4);
                        cellList.Add(range1);
                    }
                }
                foreach (CellRange range in cellList)
                {
                    tb.MergeCells(range);
                }
                
                CellRange rangeT = CellRange.Create(tb,  1, 5, rowNum-1, 5);
                tb.MergeCells(rangeT);
               
              
                // 设置列宽,行高
                for (int i = 0; i < colNum; ++i)
                {
                    if (i == 0 || i == 1)
                    {
                        tb.Columns[i].Width = 15 * scale;
                    }
                    else if (i == 2)
                    {
                        tb.Columns[i].Width = 30 * scale;
                    }
                    else
                    {
                        tb.Columns[i].Width = 25 * scale;
                    }
                }

                for (int i = 1; i < rowNum; ++i)
                {
                    tb.Rows[i].Height = 6 * scale;
                }


                tb.LayerId = lt["H细线"];
                modelSpace.AppendEntity(tb);
                tr.AddNewlyCreatedDBObject(tb, true);
                ext = ext.Add(new Extents2d(tb.Bounds.Value.MinPoint.C2D(), tb.Bounds.Value.MaxPoint.C2D()));

                List<Point2d> pList = new List<Point2d>();
                Point2d pt1 = tb.Position.C2D();
                pList.Add(pt1);
                Point2d pt2 = pt1.C2D(0, -tb.Height);
                pList.Add(pt2);
                Point2d pt3 = pt1.C2D(tb.Width, -tb.Height);
                pList.Add(pt3);
                Point2d pt4 = pt1.C2D(tb.Width, 0);
                pList.Add(pt4);
                PLPloter.AddPolylineByPointList(db, pList, ref ext, "H粗线", true);

            
                TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title, scale);          
                tr.Commit();
            }
            #endregion

        }

                
            
            
        

        /// <summary>
        /// 汇总表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="startPoint"></param>
        /// <param name="dt"></param>
        /// <param name="headName"></param>
        /// <param name="rowNum"></param>
        /// <param name="colNum"></param>
        /// <param name="tbHeight"></param>
        /// <param name="tbWidth"></param>
        /// <param name="txtStyle"></param>
        /// <param name="scale"></param>
        public static void DrawMaterialsTable(Database db, ref List<Extents2d> extLst, Point3d startPoint, System.Data.DataTable dtQu, List<string> headName,
         string txtStyle, double scale, string title)
        {
            List<Tuple<System.Data.DataTable, int, int>> tuLst = new List<Tuple<System.Data.DataTable, int, int>>();
            int tbRow = 36;
            DivTable(dtQu, tbRow, out tuLst);
            int colNum = headName.Count;
            Extents2d ext = new Extents2d(startPoint.C2D(), startPoint.C2D());
            for (int n = 0; n < tuLst.Count;n++)
            {

                System.Data.DataTable dtDiv = tuLst[n].Item1;
                startPoint = startPoint.C3D(420 * scale * n);
                ext = new Extents2d(startPoint.C2D(), startPoint.C2D());
               

                System.Data.DataTable dt= dtDiv.Clone();
                try
                {
                    //CalTableInfoByColumn(dt, "materiel", out dt);
                    CalTableInfoByColumn(dtDiv, "materiel", "materiel_code", out dt);
                }
                catch { }
                int rowNum = dt.Rows.Count + 1;
                #region 表格绘制

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                        OpenMode.ForWrite) as BlockTableRecord;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    ObjectId id;
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    id = st[txtStyle];
                    // 创建table 
                    Table tb = new Table();
                    tb.SetSize(rowNum, colNum);       // 设置几行几列
                    tb.Position = startPoint;
                    tb.GenerateLayout();

                    // 设置表头
                    for (int i = 0; i < colNum; ++i)
                    {
                        tb.Cells[0, i].TextString = headName[i]; //获取i行j列数据
                        tb.Cells[0, i].Contents[0].TextHeight = 3 * scale;
                        tb.Cells[0, i].Alignment = CellAlignment.MiddleCenter;
                        tb.Cells[0, i].Contents[0].Rotation = 0;
                        tb.Cells[0, i].Borders.Horizontal.Margin = 2 * scale;
                        tb.Cells[0, i].Contents[0].TextStyleId = id;
                        if (i == 0)
                            tb.MergeCells(CellRange.Create(tb, 0, 0, 0, 2));
                    }
                    tb.Rows[0].Height = 8 * scale;
                    string value = " ";
                    int changNum = colNum - 5;
                    if (dt.Rows.Count > 0)
                    {
                        for (int m = 1; m < rowNum; m++)
                        {
                            System.Data.DataRow dr = dt.Rows[m - 1];
                            for (int i = 0; i < colNum; ++i)
                            {
                                double weight = 0;
                                switch (i)
                                {
                                    case 0:
                                        value = dr["materiel"].ToString();
                                        break;
                                    case 1:
                                        value = dr["materiel_code"].ToString();
                                        break;
                                    case 2:
                                        value = dr["materiel_specs"].ToString().Replace("φ", "Φ").Replace("X", "×");
                                        break;
                                    case 3:
                                        value = dr["unit"].ToString();
                                        break;
                                    case 4:
                                        value = double.Parse(dr["total_weight1"].ToString()).ToString("0.000");
                                        break;
                                    case 5:
                                        value = double.Parse(dr["total_weight2"].ToString()).ToString("0.000");
                                        break;
                                    case 6:
                                        value = double.Parse(dr["total_weight3"].ToString()).ToString("0.000");
                                        break;
                                    case 7:
                                        value = double.Parse(dr["total_weight4"].ToString()).ToString("0.000");
                                        break;
                                    case 8:
                                        value = double.Parse(dr["total_weight5"].ToString()).ToString("0.000");
                                        break;
                                    case 9:
                                        value = double.Parse(dr["total_weight6"].ToString()).ToString("0.000");
                                        break;
                                    case 10:
                                        value = double.Parse(dr["total_weight7"].ToString()).ToString("0.000");
                                        break;
                                    case 11:
                                        value = double.Parse(dr["total_weight8"].ToString()).ToString("0.000");
                                        break;
                                    case 12:
                                        value = double.Parse(dr["total_weight9"].ToString()).ToString("0.000");
                                        break;
                                    case 13:
                                        value = double.Parse(dr["total_weight"].ToString()).ToString("0.000");
                                        break;
                                }
                                if (value != "0.000")
                                    tb.Cells[m, i].TextString = value; //获取i行j列数据
                                else
                                    tb.Cells[m, i].TextString = " "; //获取i行j列数据
                                tb.Cells[m, i].Contents[0].TextHeight = 2.5 * scale;
                                tb.Cells[m, i].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m, i].Contents[0].Rotation = 0;
                                tb.Cells[m, i].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m, i].Contents[0].TextStyleId = id;

                                if(dr["materiel"].ToString()== dr["materiel_code"].ToString())
                                {
                                    tb.MergeCells(CellRange.Create(tb, m, 0, m, 1));
                                }
                            }
                        }
                    }

                    // 设置列宽,行高
                    for (int i = 0; i < colNum; ++i)
                    {
                        if (i == 0 || i == 1)
                        {
                            tb.Columns[i].Width = 15 * scale;
                        }
                        else if (i == 2)
                        {
                            tb.Columns[i].Width = 30 * scale;
                        }
                        else
                        {
                            tb.Columns[i].Width = 25 * scale;
                        }
                    }

                    for (int i = 1; i < rowNum; ++i)
                    {
                        tb.Rows[i].Height = 6 * scale;
                    }
                    #region  合并单元格
                    List<Tuple<int, int>> tuMergeLst;
                    int index = 0;
                    CalMergeInfo(dt, "materiel", "materiel_code", out tuMergeLst);
                    List<CellRange> cellList = new List<CellRange>();
                    for (int i = 0; i < tuMergeLst.Count; i++)
                    {
                        CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                        cellList.Add(range);
                        CellRange range1 = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, 3, tuMergeLst[i].Item2 + 1, 3);
                        cellList.Add(range1);
                    }
                    foreach (CellRange range in cellList)
                    {
                        tb.MergeCells(range);
                    }
                    index = 1;
                    CalMergeInfo(dt, "materiel_code", out tuMergeLst);
                    cellList = new List<CellRange>();
                    for (int i = 0; i < tuMergeLst.Count; i++)
                    {
                        CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                        cellList.Add(range);
                    }
                    foreach (CellRange range in cellList)
                    {
                        tb.MergeCells(range);
                    }

                    #endregion

                    tb.LayerId = lt["H细线"];
                    modelSpace.AppendEntity(tb);
                    tr.AddNewlyCreatedDBObject(tb, true);
                    ext = ext.Add(new Extents2d(tb.Bounds.Value.MinPoint.C2D(), tb.Bounds.Value.MaxPoint.C2D()));

                    List<Point2d> pList = new List<Point2d>();
                    Point2d pt1 = tb.Position.C2D();
                    pList.Add(pt1);
                    Point2d pt2 = pt1.C2D(0, -tb.Height);
                    pList.Add(pt2);
                    Point2d pt3 = pt1.C2D(tb.Width, -tb.Height);
                    pList.Add(pt3);
                    Point2d pt4 = pt1.C2D(tb.Width, 0);
                    pList.Add(pt4);
                    PLPloter.AddPolylineByPointList(db, pList, ref ext, "H粗线", true);
                    if (n == 0)
                        TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title, scale);
                    else
                        TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title + "(续" + n + ")", scale);
                    tr.Commit();

                }
                #endregion
     
                extLst.Add(ext);
            }
        }

        public static void DrawMaterialsTableNoSubtotal(Database db, ref List<Extents2d> extLst, Point3d startPoint, System.Data.DataTable dtQu, List<string> headName,
 string txtStyle, double scale, string title)
        {
            List<Tuple<System.Data.DataTable, int, int>> tuLst = new List<Tuple<System.Data.DataTable, int, int>>();
            int tbRow = 36;
            DivTable(dtQu, tbRow, out tuLst);
            int colNum = headName.Count;
            Extents2d ext = new Extents2d(startPoint.C2D(), startPoint.C2D());
            for (int n = 0; n < tuLst.Count; n++)
            {

                System.Data.DataTable dt = tuLst[n].Item1;
                startPoint = startPoint.C3D(420 * scale * n);
                ext = new Extents2d(startPoint.C2D(), startPoint.C2D());
                int rowNum = tuLst[n].Item2 + 1;

                #region 表格绘制

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                        OpenMode.ForWrite) as BlockTableRecord;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    ObjectId id;
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    id = st[txtStyle];
                    // 创建table 
                    Table tb = new Table();
                    tb.SetSize(rowNum, colNum);       // 设置几行几列
                    tb.Position = startPoint;
                    tb.GenerateLayout();

                    // 设置表头
                    for (int i = 0; i < colNum; ++i)
                    {
                        tb.Cells[0, i].TextString = headName[i]; //获取i行j列数据
                        tb.Cells[0, i].Contents[0].TextHeight = 3 * scale;
                        tb.Cells[0, i].Alignment = CellAlignment.MiddleCenter;
                        tb.Cells[0, i].Contents[0].Rotation = 0;
                        tb.Cells[0, i].Borders.Horizontal.Margin = 2 * scale;
                        tb.Cells[0, i].Contents[0].TextStyleId = id;
                        if (i == 0)
                            tb.MergeCells(CellRange.Create(tb, 0, 0, 0, 2));
                    }
                    tb.Rows[0].Height = 8 * scale;
                    string value = " ";
                    int changNum = colNum - 5;
                    if (dt.Rows.Count > 0)
                    {
                        for (int m = 1; m < rowNum; m++)
                        {
                            System.Data.DataRow dr = dt.Rows[m - 1];
                            for (int i = 0; i < colNum; ++i)
                            {
                                double weight = 0;
                                switch (i)
                                {
                                    case 0:
                                        value = dr["materiel"].ToString();
                                        break;
                                    case 1:
                                        value = dr["materiel_code"].ToString();
                                        break;
                                    case 2:
                                        value = dr["materiel_specs"].ToString().Replace("φ", "Φ").Replace("X", "×");
                                        break;
                                    case 3:
                                        value = dr["unit"].ToString();
                                        break;
                                    case 4:
                                        value = double.Parse(dr["total_weight1"].ToString()).ToString("0.000");
                                        break;
                                    case 5:
                                        value = double.Parse(dr["total_weight2"].ToString()).ToString("0.000");
                                        break;
                                    case 6:
                                        value = double.Parse(dr["total_weight3"].ToString()).ToString("0.000");
                                        break;
                                    case 7:
                                        value = double.Parse(dr["total_weight4"].ToString()).ToString("0.000");
                                        break;
                                    case 8:
                                        value = double.Parse(dr["total_weight5"].ToString()).ToString("0.000");
                                        break;
                                    case 9:
                                        value = double.Parse(dr["total_weight6"].ToString()).ToString("0.000");
                                        break;
                                    case 10:
                                        value = double.Parse(dr["total_weight7"].ToString()).ToString("0.000");
                                        break;
                                    case 11:
                                        value = double.Parse(dr["total_weight8"].ToString()).ToString("0.000");
                                        break;
                                    case 12:
                                        value = double.Parse(dr["total_weight9"].ToString()).ToString("0.000");
                                        break;
                                    case 13:
                                        value = double.Parse(dr["total_weight"].ToString()).ToString("0.000");
                                        break;
                                }
                                if (value != "0.000")
                                    tb.Cells[m, i].TextString = value; //获取i行j列数据
                                else
                                    tb.Cells[m, i].TextString = " "; //获取i行j列数据
                                tb.Cells[m, i].Contents[0].TextHeight = 2.5 * scale;
                                tb.Cells[m, i].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m, i].Contents[0].Rotation = 0;
                                tb.Cells[m, i].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m, i].Contents[0].TextStyleId = id;
                            }
                        }
                    }

                    // 设置列宽,行高
                    for (int i = 0; i < colNum; ++i)
                    {
                        if (i == 0 || i == 1)
                        {
                            tb.Columns[i].Width = 15 * scale;
                        }
                        else if (i == 2)
                        {
                            tb.Columns[i].Width = 30 * scale;
                        }
                        else
                        {
                            tb.Columns[i].Width = 25 * scale;
                        }
                    }

                    for (int i = 1; i < rowNum; ++i)
                    {
                        tb.Rows[i].Height = 6 * scale;
                    }
                    #region  合并单元格
                    List<Tuple<int, int>> tuMergeLst;
                    int index = 0;
                    CalMergeInfo(dt, "materiel", out tuMergeLst);
                    List<CellRange> cellList = new List<CellRange>();
                    for (int i = 0; i < tuMergeLst.Count; i++)
                    {
                        CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                        cellList.Add(range);
                        CellRange range1 = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, 3, tuMergeLst[i].Item2 + 1, 3);
                        cellList.Add(range1);
                    }
                    foreach (CellRange range in cellList)
                    {
                        tb.MergeCells(range);
                    }
                    index = 1;
                    CalMergeInfo(dt, "materiel_code", out tuMergeLst);
                    cellList = new List<CellRange>();
                    for (int i = 0; i < tuMergeLst.Count; i++)
                    {
                        CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                        cellList.Add(range);
                     
                    }
                    foreach (CellRange range in cellList)
                    {
                        tb.MergeCells(range);
                    }
                    //index = 3;
                    //CalMergeInfo(dt, "unit", out tuMergeLst);
                    //cellList = new List<CellRange>();
                    //for (int i = 0; i < tuMergeLst.Count; i++)
                    //{
                    //    CellRange range = CellRange.Create(tb, tuMergeLst[i].Item1 + 1, index, tuMergeLst[i].Item2 + 1, index);
                    //    cellList.Add(range);
                    //}
                    //foreach (CellRange range in cellList)
                    //{
                    //    tb.MergeCells(range);
                    //}
                    #endregion

                    tb.LayerId = lt["H细线"];
                    modelSpace.AppendEntity(tb);
                    tr.AddNewlyCreatedDBObject(tb, true);
                    ext = ext.Add(new Extents2d(tb.Bounds.Value.MinPoint.C2D(), tb.Bounds.Value.MaxPoint.C2D()));

                    List<Point2d> pList = new List<Point2d>();
                    Point2d pt1 = tb.Position.C2D();
                    pList.Add(pt1);
                    Point2d pt2 = pt1.C2D(0, -tb.Height);
                    pList.Add(pt2);
                    Point2d pt3 = pt1.C2D(tb.Width, -tb.Height);
                    pList.Add(pt3);
                    Point2d pt4 = pt1.C2D(tb.Width, 0);
                    pList.Add(pt4);
                    PLPloter.AddPolylineByPointList(db, pList, ref ext, "H粗线", true);
                    if (n == 0)
                        TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title, scale);
                    else
                        TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title + "(续" + n + ")", scale);
                    tr.Commit();

                }
                #endregion

                extLst.Add(ext);
            }
        }
        private static void DivTable(System.Data.DataTable dt,int tbSize, out List<Tuple<System.Data.DataTable, int, int>> tuLst)
        {
            tuLst =new List<Tuple<System.Data.DataTable, int, int>>();
            if (dt.Rows.Count > 0)
            {
                System.Data.DataTable dtDiv = dt.Clone();
                int num = dt.Rows.Count / tbSize;
                int tableNum = num * tbSize >= dt.Rows.Count ? num : num + 1;
                int number = 0;
                for (int m = 0; m < tableNum; m++)
                {
                    number = 0;
                    dtDiv = dt.Clone();
                    for (int i = 0; i < tbSize; i++)
                    {
                        if (m * tbSize + i == dt.Rows.Count - 1)
                        {
                            dtDiv.Rows.Add(dt.Rows[m * tbSize + i].ItemArray);
                            number++;
                            break;
                        }
                        else
                        {
                            dtDiv.Rows.Add(dt.Rows[m * tbSize + i].ItemArray);
                            number++;
                        }
                    }
                    Tuple<System.Data.DataTable, int, int> tu = new Tuple<System.Data.DataTable, int, int>(dtDiv, number, m);
                    tuLst.Add(tu);
                }
                //foreach (Tuple<eDB, int, double> item in newLst)
                //{
                //    totalLength += item.Item3 * 0.01 * item.Item2;
                //    totalWeight += weight * item.Item3 * 0.01 * item.Item2;
                //}
            }
        }

        private static void CalMergeInfo(System.Data.DataTable dt, string colName, out List<Tuple<int,int>> tuLst)
        {
            tuLst = new List<Tuple<int, int>>();
            DataView dv0 = dt.DefaultView;
            System.Data.DataTable dtDis = dv0.ToTable(true, new string[] { colName });
            int startIndex = 0;
            for (int m = 0; m < dtDis.Rows.Count; m++)
            {
                
                string value = dtDis.Rows[m][colName].ToString();
                for (int i = startIndex; i < dt.Rows.Count; i++)
                {
                    if (i < dt.Rows.Count - 1)
                    {
                        if (dt.Rows[i][colName].ToString() != value)
                        {

                            if (startIndex > i - 1)
                            {
                                Tuple<int, int> tu = new Tuple<int, int>(startIndex, startIndex);
                                tuLst.Add(tu);
                                startIndex = (startIndex-i)+i;
                            }
                            else
                            {
                                Tuple<int, int> tu = new Tuple<int, int>(startIndex, i - 1);
                                tuLst.Add(tu);
                                startIndex = i;
                            }
                          
                            break;
                        }
                    }
                    else
                    {
                        if (dt.Rows[i][colName].ToString() != value)
                        {
                            Tuple<int, int> tu = new Tuple<int, int>(startIndex, i - 1);
                            tuLst.Add(tu);
                            startIndex = i;
                            tu = new Tuple<int, int>(startIndex, i);
                            tuLst.Add(tu);
                            break;
                        }
                        else
                        {
                            Tuple<int, int> tu = new Tuple<int, int>(startIndex, i);
                            tuLst.Add(tu);
                        }
                    }
                }
            }
        }
        private static void CalMergeInfo(System.Data.DataTable dt, string colName, string colName1, out List<Tuple<int, int>> tuLst)
        {
            tuLst = new List<Tuple<int, int>>();
            DataView dv0 = dt.DefaultView;
            System.Data.DataTable dtDis = dv0.ToTable(true, new string[] { colName, colName1 });
            int startIndex = 0;
            for (int m = 0; m < dtDis.Rows.Count; m++)
            {

                string value = dtDis.Rows[m][colName].ToString();
                string value1 = dtDis.Rows[m][colName1].ToString();
                for (int i = startIndex; i < dt.Rows.Count; i++)
                {
                    if (i < dt.Rows.Count - 1)
                    {
                        if (dt.Rows[i][colName].ToString() != value || dt.Rows[i][colName1].ToString() != value1)
                        {

                            if (startIndex > i - 1)
                            {
                                Tuple<int, int> tu = new Tuple<int, int>(startIndex, startIndex);
                                tuLst.Add(tu);
                                startIndex = (startIndex - i) + i;
                            }
                            else
                            {
                                Tuple<int, int> tu = new Tuple<int, int>(startIndex, i - 1);
                                tuLst.Add(tu);
                                startIndex = i;
                            }

                            break;
                        }
                    }
                    else
                    {
                        if (dt.Rows[i][colName].ToString() != value|| dt.Rows[i][colName1].ToString() != value1)
                        {
                            Tuple<int, int> tu = new Tuple<int, int>(startIndex, i - 1);
                            tuLst.Add(tu);
                            startIndex = i;
                            tu = new Tuple<int, int>(startIndex, i);
                            startIndex = i+1;
                            tuLst.Add(tu);
                            break;
                        }
                        else
                        {
                            Tuple<int, int> tu = new Tuple<int, int>(startIndex, i);
                            tuLst.Add(tu);
                        }
                    }
                }
            }
        }

        private static void CalTableInfoByColumn(System.Data.DataTable dt, string colName, out System.Data.DataTable dtNew)
        {
            List<Tuple<List<DataRow>, int, int>> tuLst = new List<Tuple<List<DataRow>, int, int>>();
            dtNew = dt.Clone();
            tuLst = new List<Tuple<List<DataRow>,int, int>>();
            DataView dv0 = dt.DefaultView;
            System.Data.DataTable dtDis = dv0.ToTable(true, new string[] { colName });
            List<DataRow> drLst = new List<DataRow>();
            int startIndex = 0;
            int num = 0;
            for (int m = 0; m < dtDis.Rows.Count; m++)
            {
               
                string value = dtDis.Rows[m][colName].ToString();
                for (int i = startIndex; i < dt.Rows.Count; i++)
                {
                    
                    if (i < dt.Rows.Count - 1)
                    {
                        if (dt.Rows[i][colName].ToString() != value)
                        {
                            if(drLst.Count>1)
                            {
                                DataRow dr = dt.NewRow();
                                dr["materiel"] = drLst[0]["materiel"];
                                dr["materiel_code"] = drLst[0]["materiel_code"];
                                dr["materiel_specs"] = "小计";
                                dr["unit"] = drLst[0]["unit"];
                                // 使用LINQ进行求和
                                decimal sum = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                dr["total_weight1"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                dr["total_weight2"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight2"]));
                                dr["total_weight3"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight3"]));
                                dr["total_weight4"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight4"]));
                                dr["total_weight5"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight5"]));
                                dr["total_weight6"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight6"]));
                                dr["total_weight7"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight7"]));
                                dr["total_weight8"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight8"]));
                                dr["total_weight9"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight9"]));
                                dr["total_weight"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight"]));
                                drLst.Add(dr);
                                dtNew.Rows.Add(dr.ItemArray);
                                num++;
                            }
                            
                            Tuple<List<DataRow>, int, int> tu = new Tuple<List<DataRow>, int, int>(drLst,startIndex+ num, i - 1+ num);
                            tuLst.Add(tu);
                            drLst = new List<DataRow>();
                            //drLst.Add(dt.Rows[i]);
                            startIndex = i;                        
                            break;
                        }
                        else
                        {
                            drLst.Add(dt.Rows[i]);
                            dtNew.Rows.Add(dt.Rows[i].ItemArray);
                        }
                    }
                    else
                    {
                        if (dt.Rows[i][colName].ToString() != value)
                        {
                            if (dt.Rows[i][colName].ToString() != value)
                            {
                                if (drLst.Count > 1)
                                {
                                    DataRow dr = dt.NewRow();
                                    dr["materiel"] = drLst[0]["materiel"];
                                    dr["materiel_code"] = drLst[0]["materiel_code"];
                                    dr["materiel_specs"] = "小计";
                                    dr["unit"] = drLst[0]["unit"];
                                    // 使用LINQ进行求和
                                    decimal sum = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                    dr["total_weight1"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                    dr["total_weight2"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight2"]));
                                    dr["total_weight3"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight3"]));
                                    dr["total_weight4"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight4"]));
                                    dr["total_weight5"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight5"]));
                                    dr["total_weight6"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight6"]));
                                    dr["total_weight7"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight7"]));
                                    dr["total_weight8"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight8"]));
                                    dr["total_weight9"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight9"]));
                                    dr["total_weight"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight"]));
                                    drLst.Add(dr);
                                    dtNew.Rows.Add(dr.ItemArray);
                                    num++;
                                }
                               
                                Tuple<List<DataRow>, int, int> tu = new Tuple<List<DataRow>, int, int>(drLst, startIndex + num, i - 1 + num);
                                //Tuple<List<DataRow>, int, int> tu = new Tuple<List<DataRow>, int, int>(drLst, startIndex, i - 1);
                                tuLst.Add(tu);
                                drLst = new List<DataRow>();
                                drLst.Add(dt.Rows[i]);
                                dtNew.Rows.Add(dt.Rows[i].ItemArray);
                                startIndex = i;

                                tu = new Tuple<List<DataRow>, int, int>(drLst, startIndex + num, i + num);
                                tuLst.Add(tu);
                                break;
                            }
                        }
                        else
                        {
                            drLst.Add(dt.Rows[i]);
                            dtNew.Rows.Add(dt.Rows[i].ItemArray);
                            if (drLst.Count > 1)
                            {
                                DataRow dr = dt.NewRow();
                                dr["materiel"] = drLst[0]["materiel"];
                                dr["materiel_code"] = drLst[0]["materiel_code"];
                                dr["materiel_specs"] = "小计";
                                dr["unit"] = drLst[0]["unit"];
                                // 使用LINQ进行求和
                                decimal sum = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                dr["total_weight1"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                dr["total_weight2"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight2"]));
                                dr["total_weight3"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight3"]));
                                dr["total_weight4"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight4"]));
                                dr["total_weight5"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight5"]));
                                dr["total_weight6"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight6"]));
                                dr["total_weight7"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight7"]));
                                dr["total_weight8"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight8"]));
                                dr["total_weight9"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight9"]));
                                dr["total_weight"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight"]));
                                drLst.Add(dr);
                                dtNew.Rows.Add(dr.ItemArray);
                                num++;
                            }
                          
                            Tuple<List<DataRow>, int, int> tu = new Tuple<List<DataRow>, int, int>(drLst, startIndex + num, i + num);
                            tuLst.Add(tu);
                        }
                    }
                }
            }
        }
        private static void CalTableInfoByColumn(System.Data.DataTable dt, string colName, string colName1, out System.Data.DataTable dtNew)
        {
            List<Tuple<List<DataRow>, int, int>> tuLst = new List<Tuple<List<DataRow>, int, int>>();
            dtNew = dt.Clone();
            tuLst = new List<Tuple<List<DataRow>, int, int>>();
            DataView dv0 = dt.DefaultView;
            System.Data.DataTable dtDis = dv0.ToTable(true, new string[] { colName, colName1 });
            List<DataRow> drLst = new List<DataRow>();
            int startIndex = 0;
            int num = 0;
            for (int m = 0; m < dtDis.Rows.Count; m++)
            {

                string value = dtDis.Rows[m][colName].ToString();
                string value1 = dtDis.Rows[m][colName1].ToString();
                for (int i = startIndex; i < dt.Rows.Count; i++)
                {

                    if (i < dt.Rows.Count - 1)
                    {
                        if (dt.Rows[i][colName].ToString() != value || dt.Rows[i][colName1].ToString() != value1)
                        {
                            if (drLst.Count > 1)
                            {
                                DataRow dr = dt.NewRow();
                                dr["materiel"] = drLst[0]["materiel"];
                                dr["materiel_code"] = drLst[0]["materiel_code"];
                                dr["materiel_specs"] = "小计";
                                dr["unit"] = drLst[0]["unit"];
                                // 使用LINQ进行求和
                                decimal sum = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                dr["total_weight1"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                dr["total_weight2"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight2"]));
                                dr["total_weight3"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight3"]));
                                dr["total_weight4"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight4"]));
                                dr["total_weight5"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight5"]));
                                dr["total_weight6"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight6"]));
                                dr["total_weight7"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight7"]));
                                dr["total_weight8"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight8"]));
                                dr["total_weight9"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight9"]));
                                dr["total_weight"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight"]));
                                drLst.Add(dr);
                                dtNew.Rows.Add(dr.ItemArray);
                                num++;
                            }

                            Tuple<List<DataRow>, int, int> tu = new Tuple<List<DataRow>, int, int>(drLst, startIndex + num, i - 1 + num);
                            tuLst.Add(tu);
                            drLst = new List<DataRow>();
                            //drLst.Add(dt.Rows[i]);
                            startIndex = i;
                            break;
                        }
                        else
                        {
                            drLst.Add(dt.Rows[i]);
                            dtNew.Rows.Add(dt.Rows[i].ItemArray);
                        }
                    }
                    else
                    {
                        if (dt.Rows[i][colName].ToString() != value && dt.Rows[i][colName1].ToString() != value1)
                        {
                            if (dt.Rows[i][colName].ToString() != value || dt.Rows[i][colName1].ToString() != value1)
                            {
                                if (drLst.Count > 1)
                                {
                                    DataRow dr = dt.NewRow();
                                    dr["materiel"] = drLst[0]["materiel"];
                                    dr["materiel_code"] = drLst[0]["materiel_code"];
                                    dr["materiel_specs"] = "小计";
                                    dr["unit"] = drLst[0]["unit"];
                                    // 使用LINQ进行求和
                                    decimal sum = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                    dr["total_weight1"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                    dr["total_weight2"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight2"]));
                                    dr["total_weight3"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight3"]));
                                    dr["total_weight4"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight4"]));
                                    dr["total_weight5"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight5"]));
                                    dr["total_weight6"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight6"]));
                                    dr["total_weight7"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight7"]));
                                    dr["total_weight8"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight8"]));
                                    dr["total_weight9"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight9"]));
                                    dr["total_weight"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight"]));
                                    drLst.Add(dr);
                                    dtNew.Rows.Add(dr.ItemArray);
                                    num++;
                                }
                              
                                Tuple<List<DataRow>, int, int> tu = new Tuple<List<DataRow>, int, int>(drLst, startIndex + num, i - 1 + num);
                                //Tuple<List<DataRow>, int, int> tu = new Tuple<List<DataRow>, int, int>(drLst, startIndex, i - 1);
                                tuLst.Add(tu);
                                drLst = new List<DataRow>();
                                drLst.Add(dt.Rows[i]);
                                dtNew.Rows.Add(dt.Rows[i].ItemArray);
                                startIndex = i+1;

                                tu = new Tuple<List<DataRow>, int, int>(drLst, startIndex + num, i + num);
                                tuLst.Add(tu);
                                break;
                            }
                        }
                        else
                        {
                            drLst.Add(dt.Rows[i]);
                            dtNew.Rows.Add(dt.Rows[i].ItemArray);
                            if (drLst.Count > 1)
                            {
                                DataRow dr = dt.NewRow();
                                dr["materiel"] = drLst[0]["materiel"];
                                dr["materiel_code"] = drLst[0]["materiel_code"];
                                dr["materiel_specs"] = "小计";
                                dr["unit"] = drLst[0]["unit"];
                                // 使用LINQ进行求和
                                decimal sum = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                dr["total_weight1"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight1"]));
                                dr["total_weight2"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight2"]));
                                dr["total_weight3"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight3"]));
                                dr["total_weight4"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight4"]));
                                dr["total_weight5"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight5"]));
                                dr["total_weight6"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight6"]));
                                dr["total_weight7"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight7"]));
                                dr["total_weight8"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight8"]));
                                dr["total_weight9"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight9"]));
                                dr["total_weight"] = drLst.Sum(row => Convert.ToDecimal(row["total_weight"]));
                                drLst.Add(dr);
                                dtNew.Rows.Add(dr.ItemArray);
                                num++;
                            }
                           
                            Tuple<List<DataRow>, int, int> tu = new Tuple<List<DataRow>, int, int>(drLst, startIndex + num, i + num);
                            tuLst.Add(tu);
                        }
                    }
                }
            }
        }


    }
}
